package com.newviews.tcmip.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newviews.tcmip.common.constant.CommonConsts;
import com.newviews.tcmip.common.model.PageModel;
import com.newviews.tcmip.common.model.ResultModel;
import com.newviews.tcmip.common.utils.StringUtils;
import com.newviews.tcmip.dao.EtcmDiseaseExMapper;
import com.newviews.tcmip.dao.EtcmPxExMapper;
import com.newviews.tcmip.dao.EtcmPxHerbMapper;
import com.newviews.tcmip.pojo.po.*;
import com.newviews.tcmip.dao.EtcmDiseaseMapper;
import com.newviews.tcmip.pojo.vo.EtcmCommonBaseInfoRespVO;
import com.newviews.tcmip.pojo.vo.export.EtcmDiseaseExportReqVO;
import com.newviews.tcmip.pojo.vo.index.EtcmIndexStatusticsRespVO;
import com.newviews.tcmip.service.inf.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newviews.tcmip.web.vo.etcmdiseases.DiseasePxDistanceRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.PrintStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 疾病基本信息 服务实现类
 * </p>
 *
 * @author lxc
 * @since 2024-03-01
 */
@Service
public class EtcmDiseaseServiceImpl extends ServiceImpl<EtcmDiseaseMapper, EtcmDiseasePo> implements EtcmDiseaseServiceInf {
    @Autowired
    private EtcmDiseaseExServiceInf etcmDiseaseExService;

    @Autowired
    private EtcmDiseaseExMapper etcmDiseaseExMapper;

    @Autowired
    private EtcmHerbExServiceInf etcmHerbExService;

    @Autowired
    private EtcmIngrExServiceInf etcmIngrExService;

    @Autowired
    private EtcmPxExServiceInf etcmPxExService;

    @Autowired
    private EtcmPxExMapper etcmPxExMapper;

    @Autowired
    private EtcmPxHerbMapper etcmPxHerbMapper;

    @Override
    public ResultModel<PageModel<Map<String, String>>> getIndexPage(String prepositionContent, String content, String langCode, String uiTypeCode, Integer pageNo, Integer pageSize, String sortType,String type) {
        QueryWrapper<EtcmDiseasePo> wrapper=new QueryWrapper<>();
        if (StringUtils.isBlank(type)) {
            return new ResultModel<PageModel<Map<String, String>>>().success(new PageModel<>());
        }

        String[] typeArr = type.split(CommonConsts.SPIT);
        wrapper.in("disease_type_code", Arrays.asList(typeArr));

        if(StringUtils.isNotBlank(content)){
            wrapper.and(w->w.like( "disease_zh_name", content)
                    .or().like( "disease_zh_name_py", content)
                    .or().like( "disease_zh_name_wb", content)
                    .or().like( "disease_zh_name_abb", content)
                    .or().like( "disease_en_name", content)
                    .or().like( "disease_en_name_abb", content)
                    .or().like( "disease_latin_name", content)
                    .or().like( "disease_latin_name_abb", content)
                    .or().like( "disease_name_alias", content)
            );
        }
        if(StringUtils.isNotBlank(prepositionContent)){
            wrapper.and(w->w.like( "disease_zh_name", prepositionContent)
                    .or().like( "disease_zh_name_py", prepositionContent)
                    .or().like( "disease_zh_name_wb", prepositionContent)
                    .or().like( "disease_zh_name_abb", prepositionContent)
                    .or().like( "disease_en_name", prepositionContent)
                    .or().like( "disease_en_name_abb", prepositionContent)
                    .or().like( "disease_latin_name", prepositionContent)
                    .or().like( "disease_latin_name_abb", prepositionContent)
                    .or().like( "disease_name_alias", prepositionContent)
            );
        }

        if(sortType.toLowerCase().equals(CommonConsts.SORT_TYPE_ASC.toLowerCase())){
            wrapper.orderByAsc("disease_code");
        }else {
            wrapper.orderByDesc("disease_code");
        }
        IPage<EtcmDiseasePo> etcmPxPoIPage = this.page(new Page<>(pageNo, pageSize), wrapper);
        List<Map<String,String>> list=new ArrayList<>();
        for(EtcmDiseasePo diseasePo:etcmPxPoIPage.getRecords()){
            List<EtcmDiseaseExPo> exList = etcmDiseaseExService.list(new QueryWrapper<EtcmDiseaseExPo>().eq(EtcmDiseasePo.STATUS, CommonConsts.STR_COMMON_YES).eq(EtcmDiseaseExPo.DISEASE_ID, diseasePo.getDiseaseCode()).eq(EtcmPxExPo.UI_TYPE_CODE, uiTypeCode).eq(EtcmPxExPo.LANGUAGE_CODE, langCode));
            Map<String,String> map=new HashMap<>(16);
            for(EtcmDiseaseExPo etcmDiseaseExPo:exList){
                map.put(etcmDiseaseExPo.getUiElCode(), etcmDiseaseExPo.getDiseaseExCon());
            }
            list.add(map);
        }
        PageModel<Map<String,String>> page=new PageModel<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setRecords(list);
        page.setTotalCount(etcmPxPoIPage.getTotal());
        return new ResultModel<PageModel<Map<String,String>>>().success(page);
    }

    @Override
    public ResultModel<EtcmIndexStatusticsRespVO> getStatisticsDisease(String prepositionContent, String langCode, String uiTypeCode, String type) {
        QueryWrapper<EtcmDiseasePo> wrapper=new QueryWrapper<>();
        EtcmIndexStatusticsRespVO vo = new EtcmIndexStatusticsRespVO();
        vo.setDiseases(0);
        if (StringUtils.isBlank(type)) {
            return new ResultModel<EtcmIndexStatusticsRespVO>().success(vo);
        }

        String[] typeArr = type.split(CommonConsts.SPIT);
        wrapper.in("disease_type_code", Arrays.asList(typeArr));


        if(StringUtils.isNotBlank(prepositionContent)){
            wrapper.and(w->w.like( "disease_zh_name", prepositionContent)
                    .or().like( "disease_zh_name_py", prepositionContent)
                    .or().like( "disease_zh_name_wb", prepositionContent)
                    .or().like( "disease_zh_name_abb", prepositionContent)
                    .or().like( "disease_en_name", prepositionContent)
                    .or().like( "disease_en_name_abb", prepositionContent)
                    .or().like( "disease_latin_name", prepositionContent)
                    .or().like( "disease_latin_name_abb", prepositionContent)
                    .or().like( "disease_name_alias", prepositionContent)
            );
        }


        int count = this.count(wrapper);
        vo.setDiseases(count);
        return new ResultModel<EtcmIndexStatusticsRespVO>().success(vo);
    }

    @Override
    public ResultModel<Map<String, String>> getStatistics(String content) {
        QueryWrapper<EtcmDiseasePo> wrapper=new QueryWrapper<>();
        if(StringUtils.isNotBlank(content)){
            wrapper.and(w->w.like( "disease_zh_name", content)
                    .or().like( "disease_zh_name_py", content)
                    .or().like( "disease_zh_name_wb", content)
                    .or().like( "disease_zh_name_abb", content)
                    .or().like( "disease_en_name", content)
                    .or().like( "disease_en_name_abb", content)
                    .or().like( "disease_latin_name", content)
                    .or().like( "disease_latin_name_abb", content)
                    .or().like( "disease_name_alias", content)
            );
        }
        List<EtcmDiseasePo> list = this.list(wrapper);
        Map<String, Long> map = list.stream().filter(e -> StringUtils.isNotBlank(e.getDiseaseTypeCode())).collect(Collectors.groupingBy(EtcmDiseasePo::getDiseaseTypeCode, Collectors.counting()));
        long count = list.stream().filter(e -> StringUtils.isNotBlank(e.getDiseaseTypeCode())).count();
        Map<String, String> res = getStatistics(count, map);
        return new ResultModel<Map<String, String>>().success(res);
    }

    @Override
    public List<Map<String, String>> getDiseaseDownload(EtcmDiseaseExportReqVO reqVO) {
        QueryWrapper<EtcmDiseasePo> wrapper=new QueryWrapper<>();
        if (StringUtils.isBlank(reqVO.getType())) {
            return new ArrayList<>();
        }

        String[] typeArr = reqVO.getType().split(CommonConsts.SPIT);
        wrapper.in("disease_type_code", Arrays.asList(typeArr));

        if(StringUtils.isNotBlank(reqVO.getContent())){
            wrapper.and(w->w.like( "disease_zh_name", reqVO.getContent())
                    .or().like( "disease_zh_name_py", reqVO.getContent())
                    .or().like( "disease_zh_name_wb", reqVO.getContent())
                    .or().like( "disease_zh_name_abb", reqVO.getContent())
                    .or().like( "disease_en_name", reqVO.getContent())
                    .or().like( "disease_en_name_abb", reqVO.getContent())
                    .or().like( "disease_latin_name", reqVO.getContent())
                    .or().like( "disease_latin_name_abb", reqVO.getContent())
                    .or().like( "disease_name_alias", reqVO.getContent())
            );
        }
        if(StringUtils.isNotBlank(reqVO.getPrepositionContent())){
            wrapper.and(w->w.like( "disease_zh_name", reqVO.getPrepositionContent())
                    .or().like( "disease_zh_name_py", reqVO.getPrepositionContent())
                    .or().like( "disease_zh_name_wb", reqVO.getPrepositionContent())
                    .or().like( "disease_zh_name_abb", reqVO.getPrepositionContent())
                    .or().like( "disease_en_name", reqVO.getPrepositionContent())
                    .or().like( "disease_en_name_abb", reqVO.getPrepositionContent())
                    .or().like( "disease_latin_name", reqVO.getPrepositionContent())
                    .or().like( "disease_latin_name_abb", reqVO.getPrepositionContent())
                    .or().like( "disease_name_alias", reqVO.getPrepositionContent())
            );
        }


        List<EtcmDiseasePo> etcmPxPoIPage = this.list(wrapper);
        List<EtcmDiseaseExPo> exList = etcmDiseaseExService.list(new QueryWrapper<EtcmDiseaseExPo>().eq(EtcmDiseasePo.STATUS, CommonConsts.STR_COMMON_YES).eq(EtcmPxExPo.UI_TYPE_CODE, reqVO.getUiTypeCode()).eq(EtcmPxExPo.LANGUAGE_CODE, reqVO.getLangCode()));
        Map<String,List<EtcmDiseaseExPo>> disMap = new HashMap<>(16);
        for (EtcmDiseasePo record : etcmPxPoIPage) {
            List<EtcmDiseaseExPo> disList = new ArrayList<>();
            for (EtcmDiseaseExPo etcmDiseaseExPo : exList) {
                if (etcmDiseaseExPo.getDiseaseId().equals(record.getDiseaseCode())) {
                    disList.add(etcmDiseaseExPo);
                }
            }

            disMap.put(record.getDiseaseCode(), disList);
        }
        List<EtcmUiElPo> etcmUiElPos = etcmPxExMapper.getUIElName(reqVO.getUiTypeCode(),reqVO.getLangCode());
        List<Map<String, String>> resultList = new ArrayList<>();

        for (String key : disMap.keySet()) {
            Map<String,String> mapRes=new HashMap<>(16);
            List<EtcmDiseaseExPo> values = disMap.get(key);
            List<String> collect = values.stream().map(EtcmDiseaseExPo::getUiElCode).collect(Collectors.toList());
            for (EtcmUiElPo etcmUiElPo : etcmUiElPos) {
                if (collect.contains(etcmUiElPo.getUiElCode())){
                    for (EtcmDiseaseExPo etcmDiseaseExPo : values) {
                        if (etcmUiElPo.getUiElCode().equals(etcmDiseaseExPo.getUiElCode())){
                            mapRes.put(etcmDiseaseExPo.getUiElCode(), etcmDiseaseExPo.getDiseaseExCon());
                        }
                    }
                }else {
                    mapRes.put(etcmUiElPo.getUiElCode(), "");
                }

            }

            resultList.add(mapRes);
        }

        if(reqVO.getSortType().toLowerCase().equals(CommonConsts.SORT_TYPE_ASC.toLowerCase())){
            Collections.sort(resultList, new Comparator<Map<String, String>>() {
                @Override
                public int compare(Map<String, String> o1, Map<String, String> o2) {
                    return o1.get("100716826576557218").compareTo(o2.get("100716826576557218"));
                }
            });
        }else {
            Collections.sort(resultList, new Comparator<Map<String, String>>() {
                @Override
                public int compare(Map<String, String> o1, Map<String, String> o2) {
                    return -o1.get("100716826576557218").compareTo(o2.get("100716826576557218"));
                }
            });
        }


        return resultList;
    }



    @Override
    public ResultModel<PageModel<Map<String, String>>> getPage(String content, String langCode, String uiTypeCode, Integer pageNo, Integer pageSize, String sortType,String type) {
        QueryWrapper<EtcmDiseasePo> wrapper=new QueryWrapper<>();
        if (StringUtils.isBlank(type)) {
            return new ResultModel<PageModel<Map<String, String>>>().success(new PageModel<>());
        }

        String[] typeArr = type.split(CommonConsts.SPIT);
        wrapper.in("disease_type_code", Arrays.asList(typeArr));

        if(StringUtils.isNotBlank(content)){
            wrapper.and(w->w.like( "disease_zh_name", content)
                    .or().like( "disease_zh_name_py", content)
                    .or().like( "disease_zh_name_wb", content)
                    .or().like( "disease_zh_name_abb", content)
                    .or().like( "disease_en_name", content)
                    .or().like( "disease_en_name_abb", content)
                    .or().like( "disease_latin_name", content)
                    .or().like( "disease_latin_name_abb", content)
                    .or().like( "disease_name_alias", content)
            );
        }


        if(sortType.toLowerCase().equals(CommonConsts.SORT_TYPE_ASC.toLowerCase())){
            wrapper.orderByAsc(EtcmDiseasePo.DISEASE_CODE);
        }else {
            wrapper.orderByDesc(EtcmDiseasePo.DISEASE_CODE);
        }
        IPage<EtcmDiseasePo> etcmPxPoIPage = this.page(new Page<>(pageNo, pageSize), wrapper);
        List<Map<String,String>> list=new ArrayList<>();
        for(EtcmDiseasePo diseasePo:etcmPxPoIPage.getRecords()){
            List<EtcmDiseaseExPo> exList = etcmDiseaseExService.list(new QueryWrapper<EtcmDiseaseExPo>().eq(EtcmDiseasePo.STATUS, CommonConsts.STR_COMMON_YES).eq(EtcmDiseaseExPo.DISEASE_ID, diseasePo.getDiseaseCode()).eq(EtcmPxExPo.UI_TYPE_CODE, uiTypeCode).eq(EtcmPxExPo.LANGUAGE_CODE, langCode));
            Map<String,String> map=new HashMap<>(16);
            for(EtcmDiseaseExPo etcmDiseaseExPo:exList){
                map.put(etcmDiseaseExPo.getUiElCode(), etcmDiseaseExPo.getDiseaseExCon());
            }
            list.add(map);
        }
        PageModel<Map<String,String>> page=new PageModel<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setRecords(list);
        page.setTotalCount(etcmPxPoIPage.getTotal());
        return new ResultModel<PageModel<Map<String,String>>>().success(page);
    }

    @Override
    public List<Map<String, String>> downloadDisease(String type, String sortType, String content, String langCode, String uiTypeCode) {
        QueryWrapper<EtcmDiseasePo> wrapper=new QueryWrapper<>();
        if (StringUtils.isBlank(type)) {
            return new ArrayList<>();
        }

        String[] typeArr = type.split(CommonConsts.SPIT);
        wrapper.in("t1.disease_type_code", Arrays.asList(typeArr));

        if(StringUtils.isNotBlank(content)){
            wrapper.and(w->w.like( "t1.disease_zh_name", content)
                    .or().like( "t1.disease_zh_name_py", content)
                    .or().like( "t1.disease_zh_name_wb", content)
                    .or().like( "t1.disease_zh_name_abb", content)
                    .or().like( "t1.disease_en_name", content)
                    .or().like( "t1.disease_en_name_abb", content)
                    .or().like( "t1.disease_latin_name", content)
                    .or().like( "t1.disease_latin_name_abb", content)
                    .or().like( "t1.disease_name_alias", content)
            );
        }

        wrapper.eq("t2.language_code", langCode);
        wrapper.eq("t2.ui_type_code", uiTypeCode);
//        if(sortType.toLowerCase().equals(CommonConsts.SORT_TYPE_ASC.toLowerCase())){
//            wrapper.orderByAsc("t2.disease_id");
//        }else {
//            wrapper.orderByDesc("t2.disease_id");
//        }
        wrapper.groupBy("t2.disease_id");
        List<EtcmDiseaseExPo> etcmDiseaseExPoList = etcmDiseaseExMapper.getDiseaseDownload(wrapper);
        Map<String,List<EtcmDiseaseExPo>> disMap = new HashMap<>(16);
        for (EtcmDiseaseExPo record : etcmDiseaseExPoList) {
            List<EtcmDiseaseExPo> disList = new ArrayList<>();
            String[] uielcodeList = record.getUiElCode().split("\\|\\|");
            String[] pxExconList = record.getDiseaseExCon().split("\\|\\|");



            for (int i = 0; i < uielcodeList.length; i++) {
                EtcmDiseaseExPo etcmPxExPo = new EtcmDiseaseExPo();
                for (int j = 0; j < pxExconList.length; j++) {
                    etcmPxExPo.setUiElCode(uielcodeList[i]);
                    if (i == j){
                        etcmPxExPo.setDiseaseExCon(pxExconList[j]);
                    }
                }
                disList.add(etcmPxExPo);
            }

            disMap.put(record.getDiseaseId(), disList);
        }


        List<EtcmUiElPo> etcmUiElPos = etcmPxExMapper.getUIElName(uiTypeCode,langCode);
        List<Map<String, String>> resultList = new ArrayList<>();

        for (String key : disMap.keySet()) {
            Map<String,String> mapRes=new HashMap<>(16);
            List<EtcmDiseaseExPo> values = disMap.get(key);
            List<String> collect = values.stream().map(EtcmDiseaseExPo::getUiElCode).collect(Collectors.toList());
            for (EtcmUiElPo etcmUiElPo : etcmUiElPos) {
                if (collect.contains(etcmUiElPo.getUiElCode())){
                    for (EtcmDiseaseExPo etcmPxExPo : values) {
                        if (etcmUiElPo.getUiElCode().equals(etcmPxExPo.getUiElCode())){
                            mapRes.put(etcmPxExPo.getUiElCode(), etcmPxExPo.getDiseaseExCon());
                        }
                    }
                }else {
                    mapRes.put(etcmUiElPo.getUiElCode(), "");
                }

            }

            resultList.add(mapRes);
        }
        if(sortType.toLowerCase().equals(CommonConsts.SORT_TYPE_ASC.toLowerCase())){
            Collections.sort(resultList, new Comparator<Map<String, String>>() {
                @Override
                public int compare(Map<String, String> o1, Map<String, String> o2) {
                    return o1.get("100716826576557218").compareTo(o2.get("100716826576557218"));
                }
            });
        }else {
            Collections.sort(resultList, new Comparator<Map<String, String>>() {
                @Override
                public int compare(Map<String, String> o1, Map<String, String> o2) {
                    return -o1.get("100716826576557218").compareTo(o2.get("100716826576557218"));
                }
            });
        }
        return resultList;
    }


    @Override
    public ResultModel<List<EtcmCommonBaseInfoRespVO>> getDetails(String diseaseId, String langCode, String uiTypeCode, String content) {
        List<EtcmDiseaseExPo> exPoIPage = etcmDiseaseExMapper.getPage(new QueryWrapper<EtcmDiseaseExPo>().like("t1.disease_ex_con", content).eq("t1.status", CommonConsts.STR_COMMON_YES).eq("t1.disease_id", diseaseId).eq("t1.ui_type_code", uiTypeCode).eq("t1.language_code", langCode).orderByAsc("t2.ui_el_sort"));
        List<EtcmCommonBaseInfoRespVO> list=new ArrayList<>();
        for(EtcmDiseaseExPo etcmDiseaseExPo:exPoIPage){
            EtcmCommonBaseInfoRespVO respVO=new EtcmCommonBaseInfoRespVO();
            respVO.setUiElName(etcmDiseaseExPo.getUiElName());
            respVO.setUiElValue(etcmDiseaseExPo.getDiseaseExCon());
            list.add(respVO);
        }
        return new ResultModel<List<EtcmCommonBaseInfoRespVO>>().success(list);
    }

    @Override
    public ResultModel<PageModel<Map<String, String>>> getHerb(String content, String diseaseId, String langCode, String uiTypeCode, Integer pageNo, Integer pageSize, String sortType) {
        QueryWrapper<EtcmHerbDiseasePo> wrapper=new QueryWrapper<>();
        if(StringUtils.isNotBlank(content)){
//            wrapper.and(w->w.like("t3.disease_zh_name", content)
//                    .or().like("t1.disease_en_name", content)
//                    .or().like("t1.disease_latin_name", content)
//            );
            wrapper.and(w->w.like( "t3.herb_zh_name", content)
                    .or().like( "t3.herb_zh_name_py", content)
                    .or().like( "t3.herb_zh_name_wb", content)
                    .or().like( "t3.herb_zh_name_abb", content)
                    .or().like( "t3.herb_en_name", content)
                    .or().like( "t3.herb_en_name_abb", content)
                    .or().like( "t3.herb_latin_name", content)
                    .or().like( "t3.herb_latin_name_abb", content)
                    .or().like( "t3.herb_name_alias", content)
            );
        }
        wrapper.eq("t1.disease_code", diseaseId);
//        wrapper.orderByAsc("t2.herb_disease_distance");
        wrapper.last("limit 200");


        List<EtcmHerbDiseasePo> iPage=etcmDiseaseExMapper.getDiseaseHerb( wrapper);
        List<EtcmUiElPo> uiElName = etcmPxExMapper.getUIElName(uiTypeCode, langCode);
        List<Map<String,String>> resultList=new ArrayList<>();

        if(sortType.toLowerCase().equals(CommonConsts.SORT_TYPE_ASC.toLowerCase())){
            iPage = iPage.stream().sorted(Comparator.comparing(EtcmHerbDiseasePo::getHerbDiseaseDistance)).collect(Collectors.toList());
        }else {
            iPage = iPage.stream().sorted(Comparator.comparing(EtcmHerbDiseasePo::getHerbDiseaseDistance).reversed()).collect(Collectors.toList());
        }
        List<EtcmHerbDiseasePo> collect = iPage.stream().skip((pageNo - 1) * pageSize).limit(pageSize).
                collect(Collectors.toList());


        collect.forEach(etcmHerbDiseasePo -> {
            Map<String, String> map = getStringStringMap(langCode, etcmHerbDiseasePo, uiElName);
            resultList.add(map);

        });
        PageModel<Map<String,String>> page=new PageModel<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setRecords(resultList);
        page.setTotalCount(iPage.size());
        return new ResultModel<PageModel<Map<String,String>>>().success(page);
    }

    @Override
    public ResultModel<PageModel<Map<String, String>>> getIngr(String content, String diseaseId, String langCode, String uiTypeCode, Integer pageNo, Integer pageSize, String sortType) {
        QueryWrapper<EtcmIngrDiseasePo> wrapper=new QueryWrapper<>();
        if(StringUtils.isNotBlank(content)){
            wrapper.and(w->w.like("t3.ingr_zh_name", content)
                    .or().like("t3.ingr_zh_name_py", content)
                    .or().like("t3.ingr_zh_name_wb", content)
                    .or().like("t3.ingr_zh_name_abb", content)
                    .or().like("t3.ingr_en_name", content)
                    .or().like("t3.ingr_en_name_abb", content)
                    .or().like("t3.ingr_latin_name", content)
                    .or().like("t3.ingr_latin_name_abb", content)
                    .or().like("t3.ingr_name_alias", content)
            );
        }
        wrapper.eq("t1.disease_code", diseaseId);
        wrapper.orderByAsc("t2.ingr_disease_distance");
//        wrapper.last("limit 200");


        List<EtcmIngrDiseasePo> iPage=etcmDiseaseExMapper.getDiseaseIngr(wrapper);
        List<EtcmUiElPo> uiElName = etcmPxExMapper.getUIElName(uiTypeCode, langCode);
        List<Map<String,String>> resultList=new ArrayList<>();

        if(sortType.toLowerCase().equals(CommonConsts.SORT_TYPE_ASC.toLowerCase())){
            iPage = iPage.stream().sorted(Comparator.comparing(EtcmIngrDiseasePo::getIngrDiseaseDistance)).collect(Collectors.toList());
        }else {
            iPage = iPage.stream().sorted(Comparator.comparing(EtcmIngrDiseasePo::getIngrDiseaseDistance).reversed()).collect(Collectors.toList());
        }
        List<EtcmIngrDiseasePo> collect = iPage.stream().skip((pageNo - 1) * pageSize).limit(pageSize).
                collect(Collectors.toList());
        collect.forEach(etcmIngrDiseasePo -> {
            Map<String, String> map = getStringStringMap(langCode, etcmIngrDiseasePo, uiElName);
            resultList.add(map);
        });
        PageModel<Map<String,String>> page=new PageModel<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setRecords(resultList);
        page.setTotalCount(iPage.size());
        return new ResultModel<PageModel<Map<String,String>>>().success(page);
    }

    @Override
    public ResultModel<PageModel<Map<String, String>>> getPrescription(String content, String diseaseId, String langCode, String uiTypeCode, Integer pageNo, Integer pageSize, String sortType) {
        QueryWrapper<DiseasePxDistanceRespVo> wrapper=new QueryWrapper<>();
        if(StringUtils.isNotBlank(content)){
//            wrapper.and(w->w.like("t4.disease_zh_name", content)
//                    .or().like("t1.disease_en_name", content)
//                    .or().like("t1.disease_latin_name", content)
//            );
            wrapper.and(w->w.like("t4.px_zh_name", content)
                    .or().like("t4.px_zh_name_py", content)
                    .or().like("t4.px_zh_name_wb", content)
                    .or().like("t4.px_zh_name_abb", content)
                    .or().like("t4.px_en_name", content)
                    .or().like("t4.px_en_name_abb", content)
                    .or().like("t4.px_latin_name", content)
                    .or().like("t4.px_latin_name_abb", content)
                    .or().like("t4.px_name_alias", content)
            );
        }
        wrapper.eq("t1.disease_code", diseaseId);
        //wrapper.orderByAsc("t2.herb_disease_distance");
        wrapper.last("limit 200");

        List<DiseasePxDistanceRespVo> iPage=etcmDiseaseExMapper.getDiseasePx( wrapper);

        List<EtcmUiElPo> uiElName = etcmPxExMapper.getUIElName(uiTypeCode, langCode);
        List<Map<String,String>> resultList=new ArrayList<>();

        if(sortType.toLowerCase().equals(CommonConsts.SORT_TYPE_ASC.toLowerCase())){
            iPage = iPage.stream().sorted(Comparator.comparing(DiseasePxDistanceRespVo::getHerbDiseaseDistance)).collect(Collectors.toList());
        }else {
            iPage = iPage.stream().sorted(Comparator.comparing(DiseasePxDistanceRespVo::getHerbDiseaseDistance).reversed()).collect(Collectors.toList());
        }
        List<DiseasePxDistanceRespVo> collect = iPage.stream().skip((pageNo - 1) * pageSize).limit(pageSize).
                collect(Collectors.toList());
        collect.forEach(etcmIngrDiseasePo -> {
            Map<String, String> map = getStringStringMap(langCode, etcmIngrDiseasePo, uiElName);
            resultList.add(map);
        });
        PageModel<Map<String,String>> page=new PageModel<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setRecords(resultList);
        page.setTotalCount(iPage.size());
        return new ResultModel<PageModel<Map<String,String>>>().success(page);
    }

    @Override
    public List<Map<String, String>> getDiseaseList(String content, String langCode, String uiTypeCode) {
        QueryWrapper<EtcmDiseasePo> wrapper=new QueryWrapper<>();
        if(StringUtils.isNotBlank(content)){
            wrapper.and(w->w.like(StringUtils.isNotBlank(content), EtcmDiseasePo.DISEASE_ZH_NAME, content)
                    .or().like(StringUtils.isNotBlank(content), EtcmDiseasePo.DISEASE_EN_NAME, content)
                    .or().like(StringUtils.isNotBlank(content), EtcmDiseasePo.DISEASE_ZH_NAME_ABB, content)
                    .or().like(StringUtils.isNotBlank(content), EtcmDiseasePo.DISEASE_EN_NAME_ABB, content)
                    .or().like(StringUtils.isNotBlank(content), EtcmDiseasePo.DISEASE_LATIN_NAME_ABB, content)
                    .or().like(StringUtils.isNotBlank(content), EtcmDiseasePo.DISEASE_LATIN_NAME, content));
        }
        List<EtcmDiseasePo> resultList = this.list(wrapper);
        List<Map<String,String>> list=new ArrayList<>();
        for(EtcmDiseasePo diseasePo:resultList){
            List<EtcmDiseaseExPo> exList = etcmDiseaseExMapper.getDiseaseList(new QueryWrapper<EtcmDiseaseExPo>().eq(EtcmDiseasePo.STATUS, CommonConsts.STR_COMMON_YES).eq(EtcmDiseaseExPo.DISEASE_ID, diseasePo.getId()).eq(EtcmPxExPo.UI_TYPE_CODE, uiTypeCode).eq(EtcmPxExPo.LANGUAGE_CODE, langCode));
            Map<String,String> map=new HashMap<>(16);
            for(EtcmDiseaseExPo etcmDiseaseExPo:exList){
                map.put(etcmDiseaseExPo.getUiElName(), etcmDiseaseExPo.getDiseaseExCon());
            }
            list.add(map);
        }
       return list;
    }



    @Override
    public List<Map<String, String>> getDiseaseDetails(String diseaseId, String content, String langCode, String uiTypeCode) {
        QueryWrapper<EtcmDiseaseExPo> wrapper=new QueryWrapper<>();
        wrapper.eq("t1.disease_id", diseaseId);
        wrapper.eq("t2.ui_type_code", uiTypeCode);
        wrapper.eq("t2.language_Code", langCode);
        wrapper.orderByAsc("t2.ui_el_sort");
        List<EtcmDiseaseExPo> resultList = etcmDiseaseExMapper.getDiseaseDetailsList(wrapper);
        List<Map<String,String>> list=new ArrayList<>();
        Map<String,String> map=new LinkedHashMap<>(16);
        for(EtcmDiseaseExPo etcmDiseaseExPo:resultList){
            map.put(etcmDiseaseExPo.getUiElName(), etcmDiseaseExPo.getDiseaseExCon());
        }
        list.add(map);
        return list;
    }

    @Override
    public List<Map<String, String>> getPrescriptionList(String content, String diseaseId, String langCode, String uiTypeCode,String sortType) {
        QueryWrapper<DiseasePxDistanceRespVo> wrapper=new QueryWrapper<>();
        if(StringUtils.isNotBlank(content)){
//            wrapper.and(w->w.like("t4.disease_zh_name", content)
//                    .or().like("t1.disease_en_name", content)
//                    .or().like("t1.disease_latin_name", content)
//            );
            wrapper.and(w->w.like("t4.px_zh_name", content)
                    .or().like("t4.px_zh_name_py", content)
                    .or().like("t4.px_zh_name_wb", content)
                    .or().like("t4.px_zh_name_abb", content)
                    .or().like("t4.px_en_name", content)
                    .or().like("t4.px_en_name_abb", content)
                    .or().like("t4.px_latin_name", content)
                    .or().like("t4.px_latin_name_abb", content)
                    .or().like("t4.px_name_alias", content)
            );
        }
        wrapper.eq("t1.disease_code", diseaseId);
        //wrapper.orderByAsc("t2.herb_disease_distance");
        wrapper.last("limit 200");

        List<DiseasePxDistanceRespVo> iPage=etcmDiseaseExMapper.getDiseasePx( wrapper);

        List<EtcmUiElPo> uiElName = etcmPxExMapper.getUIElName(uiTypeCode, langCode);
        List<Map<String,String>> resultList=new ArrayList<>();

        if(sortType.toLowerCase().equals(CommonConsts.SORT_TYPE_ASC.toLowerCase())){
            iPage = iPage.stream().sorted(Comparator.comparing(DiseasePxDistanceRespVo::getHerbDiseaseDistance)).collect(Collectors.toList());
        }else {
            iPage = iPage.stream().sorted(Comparator.comparing(DiseasePxDistanceRespVo::getHerbDiseaseDistance).reversed()).collect(Collectors.toList());
        }

        iPage.forEach(etcmIngrDiseasePo -> {
            Map<String, String> map = getStringStringMap(langCode, etcmIngrDiseasePo, uiElName);
            resultList.add(map);
        });

        return resultList;
    }

    @Override
    public List<Map<String, String>> getHerbList(String content, String diseaseId, String langCode, String uiTypeCode,String sortType) {
        QueryWrapper<EtcmHerbDiseasePo> wrapper=new QueryWrapper<>();
        if(StringUtils.isNotBlank(content)){
//            wrapper.and(w->w.like("t3.disease_zh_name", content)
//                    .or().like("t1.disease_en_name", content)
//                    .or().like("t1.disease_latin_name", content)
//            );
            wrapper.and(w->w.like( "t3.herb_zh_name", content)
                    .or().like( "t3.herb_zh_name_py", content)
                    .or().like( "t3.herb_zh_name_wb", content)
                    .or().like( "t3.herb_zh_name_abb", content)
                    .or().like( "t3.herb_en_name", content)
                    .or().like( "t3.herb_en_name_abb", content)
                    .or().like( "t3.herb_latin_name", content)
                    .or().like( "t3.herb_latin_name_abb", content)
                    .or().like( "t3.herb_name_alias", content)
            );
        }
        wrapper.eq("t1.disease_code", diseaseId);
//        wrapper.orderByAsc("t2.herb_disease_distance");
//        wrapper.last("limit 200");


        List<EtcmHerbDiseasePo> iPage=etcmDiseaseExMapper.getDiseaseHerb( wrapper);
        List<EtcmUiElPo> uiElName = etcmPxExMapper.getUIElName(uiTypeCode, langCode);
        List<Map<String,String>> resultList=new ArrayList<>();

        if(sortType.toLowerCase().equals(CommonConsts.SORT_TYPE_ASC.toLowerCase())){
            iPage = iPage.stream().sorted(Comparator.comparing(EtcmHerbDiseasePo::getHerbDiseaseDistance)).collect(Collectors.toList());
        }else {
            iPage = iPage.stream().sorted(Comparator.comparing(EtcmHerbDiseasePo::getHerbDiseaseDistance).reversed()).collect(Collectors.toList());
        }

        iPage.forEach(etcmHerbDiseasePo -> {
            Map<String, String> map = getStringStringMap(langCode, etcmHerbDiseasePo, uiElName);
            resultList.add(map);

        });
        return resultList;
    }

    @Override
    public List<Map<String, String>> getIngrList(String content,String diseaseId, String langCode, String uiTypeCode,String sortType) {
        QueryWrapper<EtcmIngrDiseasePo> wrapper=new QueryWrapper<>();
        if(StringUtils.isNotBlank(content)){
            wrapper.and(w->w.like("t3.ingr_zh_name", content)
                    .or().like("t3.ingr_zh_name_py", content)
                    .or().like("t3.ingr_zh_name_wb", content)
                    .or().like("t3.ingr_zh_name_abb", content)
                    .or().like("t3.ingr_en_name", content)
                    .or().like("t3.ingr_en_name_abb", content)
                    .or().like("t3.ingr_latin_name", content)
                    .or().like("t3.ingr_latin_name_abb", content)
                    .or().like("t3.ingr_name_alias", content)
            );
        }
        wrapper.eq("t1.disease_code", diseaseId);
        wrapper.orderByAsc("t2.ingr_disease_distance");
//        wrapper.last("limit 200");


        List<EtcmIngrDiseasePo> iPage=etcmDiseaseExMapper.getDiseaseIngr(wrapper);
        List<EtcmUiElPo> uiElName = etcmPxExMapper.getUIElName(uiTypeCode, langCode);
        List<Map<String,String>> resultList=new ArrayList<>();

        if(sortType.toLowerCase().equals(CommonConsts.SORT_TYPE_ASC.toLowerCase())){
            iPage = iPage.stream().sorted(Comparator.comparing(EtcmIngrDiseasePo::getIngrDiseaseDistance)).collect(Collectors.toList());
        }else {
            iPage = iPage.stream().sorted(Comparator.comparing(EtcmIngrDiseasePo::getIngrDiseaseDistance).reversed()).collect(Collectors.toList());
        }
        iPage.forEach(etcmIngrDiseasePo -> {
            Map<String, String> map = getStringStringMap(langCode, etcmIngrDiseasePo, uiElName);
            resultList.add(map);
        });

        return resultList;
    }

    @Override
    public ResultModel<List<EtcmDiseaseTypePo>> getDiseaseTypeList(String langCode) {
        List<EtcmDiseaseTypePo> list = etcmDiseaseExMapper.getDiseaseTypeList(langCode);
        return new ResultModel<List<EtcmDiseaseTypePo>>().success(list);
    }




    private Map<String, String> getStatistics(long totalNumber,Map<String, Long> map){
        Map<String,String> mapRes = new HashMap<>(16);
        map.keySet().forEach(s -> {
            BigDecimal totalCount = new BigDecimal(totalNumber);
            BigDecimal size = new BigDecimal(map.get(s));
            BigDecimal bigDecimal = size.divide(totalCount,2, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
            mapRes.put(s,bigDecimal.toString());
        });
        return mapRes;
    }


    private static Map<String, String> getStringStringMap(String langCode, EtcmHerbDiseasePo etcmHerbDiseasePo, List<EtcmUiElPo> uiElNameList) {
        Map<String,String> map = new HashMap<>(16);
        for (EtcmUiElPo etcmUiElPo : uiElNameList) {
            switch (etcmUiElPo.getUiElSort()){
                case 1:
                    map.put(etcmUiElPo.getUiElCode(), etcmHerbDiseasePo.getDiseaseId() + "-" + etcmHerbDiseasePo.getHerbId());
                    break;
                case 2:
                    if (CommonConsts.LANG_CODE_ENG.equals(langCode)){
                        map.put(etcmUiElPo.getUiElCode(), etcmHerbDiseasePo.getDiseaseEnName());
                    }
                    if (CommonConsts.LANG_CODE_CHN.equals(langCode)){
                        map.put(etcmUiElPo.getUiElCode(), etcmHerbDiseasePo.getDiseaseZhName());
                    }
                    break;

                case 3:
                    if (CommonConsts.LANG_CODE_ENG.equals(langCode)){
                        map.put(etcmUiElPo.getUiElCode(), etcmHerbDiseasePo.getHerbEnName());
                    }
                    if (CommonConsts.LANG_CODE_CHN.equals(langCode)){
                        map.put(etcmUiElPo.getUiElCode(), etcmHerbDiseasePo.getHerbZhName());
                    }
                    break;
                case 4:
                    map.put(etcmUiElPo.getUiElCode(), etcmHerbDiseasePo.getHerbDiseaseDistance());
                    break;
                case 5:
                    map.put(etcmUiElPo.getUiElCode(),etcmHerbDiseasePo.getPValue());
                default:
            }

        }
        return map;
    }

    private static Map<String, String> getStringStringMap(String langCode, EtcmIngrDiseasePo etcmIngrDiseasePo, List<EtcmUiElPo> uiElNameList) {
        Map<String,String> map = new HashMap<>(16);
        for (EtcmUiElPo etcmUiElPo : uiElNameList) {
            switch (etcmUiElPo.getUiElSort()){
                case 1:
                    map.put(etcmUiElPo.getUiElCode(), etcmIngrDiseasePo.getDiseaseId() + "-" + etcmIngrDiseasePo.getIngrId());
                    break;
                case 2:
                    if (CommonConsts.LANG_CODE_ENG.equals(langCode)){
                        map.put(etcmUiElPo.getUiElCode(), etcmIngrDiseasePo.getDiseaseEnName());
                    }
                    if (CommonConsts.LANG_CODE_CHN.equals(langCode)){
                        map.put(etcmUiElPo.getUiElCode(), etcmIngrDiseasePo.getDiseaseZhName());
                    }
                    break;

                case 3:
                    if (CommonConsts.LANG_CODE_ENG.equals(langCode)){
                        map.put(etcmUiElPo.getUiElCode(), etcmIngrDiseasePo.getIngrEnName());
                    }
                    if (CommonConsts.LANG_CODE_CHN.equals(langCode)){
                        map.put(etcmUiElPo.getUiElCode(), etcmIngrDiseasePo.getIngrZhName());
                    }
                    break;
                case 4:
                    map.put(etcmUiElPo.getUiElCode(), etcmIngrDiseasePo.getIngrDiseaseDistance());
                    break;
                case 5:
                    map.put(etcmUiElPo.getUiElCode(),etcmIngrDiseasePo.getPValue());
                default:
            }

        }
        return map;
    }

    private static Map<String, String> getStringStringMap(String langCode, DiseasePxDistanceRespVo diseasePxDistanceRespVo, List<EtcmUiElPo> uiElNameList) {
        Map<String,String> map = new HashMap<>(16);
        for (EtcmUiElPo etcmUiElPo : uiElNameList) {
            switch (etcmUiElPo.getUiElSort()){
                case 1:
                    map.put(etcmUiElPo.getUiElCode(), diseasePxDistanceRespVo.getDiseaseId() + "-" + diseasePxDistanceRespVo.getPxId());
                    break;
                case 2:
                    if (CommonConsts.LANG_CODE_ENG.equals(langCode)){
                        map.put(etcmUiElPo.getUiElCode(), diseasePxDistanceRespVo.getDiseaseEnName());
                    }
                    if (CommonConsts.LANG_CODE_CHN.equals(langCode)){
                        map.put(etcmUiElPo.getUiElCode(), diseasePxDistanceRespVo.getDiseaseZhName());
                    }
                    break;

                case 3:
                    if (CommonConsts.LANG_CODE_ENG.equals(langCode)){//因处方没有英文，直接展示拼音
                        //map.put(etcmUiElPo.getUiElCode(), diseasePxDistanceRespVo.getPxEnName());
                        map.put(etcmUiElPo.getUiElCode(), diseasePxDistanceRespVo.getPxZhNamePy());
                    }
                    if (CommonConsts.LANG_CODE_CHN.equals(langCode)){
                        map.put(etcmUiElPo.getUiElCode(), diseasePxDistanceRespVo.getPxZhName());
                    }
                    break;
                case 4:
                    map.put(etcmUiElPo.getUiElCode(), diseasePxDistanceRespVo.getHerbDiseaseDistance());
                    break;
                case 5:
                    map.put(etcmUiElPo.getUiElCode(),diseasePxDistanceRespVo.getPValue());
                default:
            }

        }
        return map;
    }
}
